package cn.jsj.onclock.controller;

import cn.jsj.onclock.pojo.UserJoinRequest;
import cn.jsj.onclock.pojo.identity.ApproveResultEnum;
import cn.jsj.onclock.result.Result;
import cn.jsj.onclock.service.UserJoinRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.jsj.onclock.interceptor.RequestThreadContext.requesterUserId;

/**
 * @author 卢斌
 */
@Controller
@CrossOrigin
@ResponseBody
@RequestMapping(value = "/user_join")
public class UserRequestJoinGroupController {

    private final UserJoinRequestService userJoinRequestService;

    @Autowired
    public UserRequestJoinGroupController(UserJoinRequestService userJoinRequestService) {
        this.userJoinRequestService = userJoinRequestService;
    }


    /**
     * 用户提交个人加群申请
     */
    @PostMapping(value = "/request")
    public Result<ApproveResultEnum> submit(@RequestBody UserJoinRequest joinRequest) {
        ApproveResultEnum requestResult = userJoinRequestService.submitUserJoinGroupRequest(requesterUserId(), joinRequest.getAimGroup(), joinRequest.getMessage(), joinRequest.getLeaveName());
        return Result.success(requestResult);
    }


    /**
     * 用户查看自己的加群申请
     */
    @GetMapping(value = "/user_request")
    public Result<UserJoinRequest> listAboutUser(@RequestParam(value = "result", required = false) ApproveResultEnum result, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Long userId =  requesterUserId();
        List<UserJoinRequest> storage = userJoinRequestService.listUserJoinRequest(userId, result, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 获取群组收到的加群申请信息
     */
    @GetMapping(value = "/group_request", params = {"groupId", "result", "pageNum", "pageSize"})
    public Result<UserJoinRequest> listAboutGroup(@RequestParam("groupId") Long groupId, @RequestParam(value = "result") ApproveResultEnum result, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Long userId = requesterUserId();
        List<UserJoinRequest> storage = userJoinRequestService.listGroupReceivedUserJoinRequest(userId, groupId, result, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 群相关负责人审批普通用户提交的加群申请信息
     */
    @GetMapping(value = "/judge_request", params = {"requestId", "result"})
    public Result<ApproveResultEnum> judgeRequest(@RequestParam("requestId") Long requestId, @RequestParam("result") ApproveResultEnum result) {
        Long userId = requesterUserId();
        ApproveResultEnum resultEnum = userJoinRequestService.judgeGroupRequest(userId, requestId, result);
        return Result.success(resultEnum);
    }


}
